package easy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

/*
349. 两个数组的交集
 */
public class LeeCode349 {
  public static void main(String[] args) {
    new LeeCode349().intersection(new int[]{4,9,5},new int[]{9,4,9,8,4});
  }

  public int[] intersection(int[] nums1, int[] nums2) {
    HashSet<Integer> set1 = new HashSet<>();
    HashSet<Integer> set2 = new HashSet<>();
    ArrayList<Integer> list = new ArrayList<>();
    for (int i : nums1) {
      set1.add(i);
    }
    for (int i : nums2) {
      set2.add(i);
    }
    Iterator<Integer> iterator = set1.iterator();
    while (iterator.hasNext()){
      Integer next = iterator.next();
      if (set2.contains(next)){
        list.add(next);
      }
    }
    int[] res = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
      res[i] = list.get(i);
    }
    return res;
  }
}
